/*
 * Copyright (C) 2012 Johan Boris Iantila jiantila@gmail.com && http://www.facebook.com/johanboris.iantila
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package mg.daloji.antandroyd.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import mg.daloji.antandroyd.CONSTANTS;
import mg.daloji.antandroyd.R;
import mg.daloji.antandroyd.Radio;

import mg.daloji.antandroyd.application.App;
import mg.daloji.antandroyd.service.ServiceRadio;

import mg.daloji.antandroyd.ui.ActionBarView;
import mg.daloji.antandroyd.ui.LazyAdapter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class Main extends Activity {
	// All static variables
	static final String URL = "http://api.androidhive.info/music/music.xml";
	// XML node keys

	static final String KEY_SONG = "radio"; // parent node
	static final String KEY_ID = "id";
	public static  String KEY_TITLE = "title";
	public static  String KEY_ARTIST = "artist";
	public static String KEY_DURATION = "duration";
	public static  String KEY_THUMB_URL = "thumb_url";
	private ActionBarView	mActionBar;
	private Boolean played=false;
	private TextView		mTvClick;
	private ProgressDialog mWaitDialog;
	private int radioselected;
	private Messenger messenger = null;
	ListView list;
	LazyAdapter adapter;
	LinkedList<Radio> listRadio;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//mActionBar=(ActionBarView)findViewById(R.id.actionbar);
		//mActionBar.showButtonSearch();
		listRadio=App.getConf();
		list=(ListView)findViewById(R.id.list);
		adapter=new LazyAdapter(this, listRadio);        
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {	
				radioselected=position;
				Intent intent =new Intent(getApplicationContext(),Home.class);
				intent.putExtra(CONSTANTS.INDEX,radioselected);
				startActivity(intent);

			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
/*
	/**
	 * Dismisses the wait dialog if any
	 */
	void hideWaitDialog() {
		if (null != this.mWaitDialog) {
			this.mWaitDialog.dismiss();
		}
	}

	
	private ServiceConnection conn = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			  messenger = new Messenger(service);		
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
		    messenger = null;
		}
	  };
	  /**
	   * 
	   * @author daloji
	   *
	   */
	  public class Receiver extends BroadcastReceiver {
			@Override
			public void onReceive(Context context, Intent intent) {
				hideWaitDialog();
			}
			
		}
	  

	  public final boolean isInternetOn()
	  {
		  boolean state=false;
		  ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		  if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
				  connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ){
			  state= true;
		  }
		  else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
				  ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  )
		  {
			  state=false;
		  }
		  return state;
	  }
}